package cn.niit.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//动态模式
public class DynamicProxy implements InvocationHandler {
    //定义一个成员变量
    private  Object target;
    //提供一个 有参的构造函数
    public DynamicProxy(Object target) {
        this.target = target;
    }

   public  Object getProxy(){
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                //匿名内部类
                new InvocationHandler(){
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("即将调用" + method.getName());
                        Object returnVal = method.invoke(target,args);
                        System.out.println(returnVal);
                        System.out.println("已经完成" + method.getName() + "的调用。");
                        System.out.println("--------------"+ method.getName() + "---------------------");
                        return returnVal;
                    }
                }

        );
   }
    //实现接口的方法
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return null;
    }
}
